﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace FinWeb3.Models.Repository
{
    public class SnapshotLineRepository : IRepository<SnapshotLine>
    {
        public void Save(SnapshotLine item, IIdentity user)
        {
            if (item.Id == 0)
            {
                item.UserName = user.Name;
                item.Save();
            }
            else
            {
                var snapshotLine = Get(item.Id, user);

                if (snapshotLine != null)
                {
                    snapshotLine.BankId = item.BankId;
                    snapshotLine.Category = item.Category;
                    snapshotLine.Day = item.Day;
                    snapshotLine.Description = item.Description;
                    snapshotLine.Value = item.Value;
                    snapshotLine.HasDetailList = item.HasDetailList;
                    snapshotLine.UserName = user.Name;
                    snapshotLine.ReceiveBill = item.ReceiveBill;
                    snapshotLine.ParentBillId = item.ParentBillId;

                    snapshotLine.Save();
                }
            }
        }

        public void Delete(SnapshotLine item, IIdentity user)
        {
            var snapshotLine = Get(item.Id, user);

            if (snapshotLine != null)
                snapshotLine.Delete();
        }

        public SnapshotLine Get(int id, IIdentity user)
        {
            return SnapshotLine.All().Where(x => x.UserName == user.Name && x.Id == id).FirstOrDefault();
        }

        public IQueryable<SnapshotLine> GetAll(IIdentity user)
        {
            return SnapshotLine.All().Where(x => x.UserName == user.Name);
        }
    }
}
